/*
 * @lc app=leetcode.cn id=1 lang=java
 *
 * [1] 两数之和
 */

// @lc code=start
class Solution {
    public static int[] twoSum(int[] nums, int target) {
        Map<Integer,List<Integer>> numsMap = new HashMap<>();
        int[] result = null;
        for (int i = 0; i < nums.length; i++) {
            int b = nums[i];
            int c = target-b;
            if(numsMap.containsKey(c)) {
                final Integer if1 = i;
                List<Integer> idxList = numsMap.get(c)
                        .stream().filter(e->e<if1).collect(Collectors.toList());
                if(!idxList.isEmpty()) {
                    result = new int[]{idxList.get(0),i};
                }
            }else {
                numsMap.put(b,new ArrayList<>(Collections.singleton(i))); 
            }
        }
        return result;
    }
}
// @lc code=end

